package com.elvin.zoe.pojo;

import java.text.ParseException;
import java.util.Date;

public class ImMessage extends TextMessage {

    public static final String LINE_BREAK = "<BR>";
    private static final int TIME_SPAN_MINUTES = 5;
    private Date latestDatetime = null;
    private int index;

    public ImMessage() {
        super();
    }

    public ImMessage(int type, String datetimeString, String datetimeFormat, String text, int index ) throws ParseException {
        super();
        this.type = type;
        this.text = text;
        this.datetimeFormat = datetimeFormat;
        setDatetimeString(datetimeString);
        this.index = index;

        this.latestDatetime = this.getDateTime();
    }

    public boolean merge(ImMessage imMessage){
        if(imMessage == null)
            return false;

        if(imMessage.getType() != this.getType())
            return false;

        long timeDifference = imMessage.getDateTime().getTime() - latestDatetime.getTime();
        if(timeDifference / 1000L  < 60 * TIME_SPAN_MINUTES){
            latestDatetime = imMessage.getDateTime();
            this.text += LINE_BREAK + imMessage.getText();
            return true;
        }
        return false;

    }

    public int getIndex() {
        return index;
    }

}
